import axios, { AxiosRequestConfig } from 'axios'

import {
  IAddNoteReq,
  IAddressListReq,
  IAddressListRes,
  ICargoCommentInfo,
  IFirstLegInformationRes,
  IGetBoxNumberMapInformationReq,
  IGetDetailReq,
  IGetNoteReq,
  IGetWarehouseListReq,
  IGetWaybillAttachDetailReq,
  IOrderProductBoxInfoRes,
  ISetFirstLegInformationReq,
  ISetIsaBlReq,
  ISetLastLegInformationReq,
  ISetLogisticsChannelReq,
  ISetLogisticsProductReq,
  ISetPublishEventReq,
  ISetTransferWarehouseReq,
  IUploadWaybillAttachReq,
  IUploadWaybillAttachRes,
  IWarehouseListRes,
  IWaybillAttachDetailRes,
  IWaybillDestinationAddressRes,
  IWaybillDetailRes,
  IWaybillOriginAddressRes,
  IWaybillPublishPathRes,
  IWaybillStatusOverviewRes,
  IWaybillTransferWarehouseInformationRes,
} from '../interface'
import './__mock__'

/** 获取运单详情 */
export const apiGetWaybillDetail = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/waybill_detail`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 添加备注 */
export const addComment = async (req: ICargoCommentInfo): Promise<void> => {
  await axios.post(`/fba_otb/cargo/addComment`, req)
}

/** 设置物流产品 */
export const apiSetLogisticsProduct = async (
  req: ISetLogisticsProductReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/set/logistics_product`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 设置物流渠道 */
export const apiSetLogisticsChannel = async (
  req: ISetLogisticsChannelReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/set/logistics_channel`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 获取头程信息 */
export const apiGetFirstLegInformation = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IFirstLegInformationRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/waybill_first_leg_information`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 设置头程信息 */
export const apiSetFirstLegInformation = async (
  req: ISetFirstLegInformationReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/set/first_leg_information`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 设置尾程信息 */
export const apiSetLastLegInformation = async (
  req: ISetLastLegInformationReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/set/last_mile`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 获取运单发布路径 */
export const apiGetWaybillPublishPath = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillPublishPathRes[]> => {
  return axios.post(`/tmsapi/waybill_record_v2/waybill_publish_path`, { ...req }, { signal }).then(res => res.$data)
}

/** 获取运单中转仓信息 */
export const apiGetWaybillTransferWarehouseInformation = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillTransferWarehouseInformationRes> => {
  return axios
    .post(`/tmsapi/waybill_record_v2/waybill_transfer_warehouse_information`, { ...req }, { signal })
    .then(res => ({
      ...res.$data,
    }))
}

/** 获取运单附件详情 */
export const apiGetWaybillAttachDetail = async (
  req: IGetWaybillAttachDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillAttachDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/waybill_attach_detail`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 上传运单附件 */
export const apiUploadWaybillAttach = async (
  req: IUploadWaybillAttachReq & { userId: number },
  signal?: AxiosRequestConfig['signal'],
): Promise<IUploadWaybillAttachRes> => {
  const { userId, ...rest } = req
  return axios({
    url: `/tmsapi/waybill_record_v2/uploadWaybillAttach`,
    method: 'POST',
    data: rest,
    params: { userId },
    signal,
  }).then(res => res.$data)
}

/** 设置集货仓/中转仓 */
export const apiSetWarehouse = async (
  req: ISetTransferWarehouseReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/set/warehouse`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 添加备注 */
export const apiAddNote = async (
  req: IAddNoteReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/set/note`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 获取备注 */
export const apiGetNote = async (req: IGetDetailReq, signal?: AxiosRequestConfig['signal']): Promise<IGetNoteReq[]> => {
  return axios.post(`/tmsapi/waybill_record_v2/get/note`, { ...req }, { signal }).then(res => res.$data)
}

/** 获取运单箱号映射信息 */
export const apiGetBoxNumberMapInformation = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IGetBoxNumberMapInformationReq[]> => {
  return axios
    .post(`/tmsapi/waybill_record_v2/box_number_map_information`, { ...req }, { signal })
    .then(res => res.$data || [])
}

/** 导入运单箱号映射信息 */
export const apiImportBoxNumberMap = async (
  req: { file: File; waybillNo: string },
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  const formData = new FormData()
  formData.append('file', req.file)
  return axios({
    url: `/tmsapi/waybill_record_v2/box_number_map/import`,
    method: 'POST',
    data: formData,
    params: { waybillNo: req.waybillNo },
    signal,
  }).then(res => res.$data)
}

/** 获取中转仓列表 */
export const apiGetWarehouseList = async (
  req: IGetWarehouseListReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWarehouseListRes[]> => {
  return axios.post(`/tmsapi/waybill_record_v2/warehouse/list`, { ...req }, { signal }).then(res => res.$data)
}

/** 设置是否为ISA/BL */
export const apiSetIsaBl = async (
  req: ISetIsaBlReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/set/isa_bl`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 获取运单状态概览 */
export const apiGetWaybillStatusOverview = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillStatusOverviewRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/waybill_status_overview`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 获取订单商品箱信息 */
export const apiGetOrderProductBoxInfo = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IOrderProductBoxInfoRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/getOrderProductBoxInfo`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 设置发布事件 */
export const apiSetPublishEvent = async (
  req: ISetPublishEventReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDetailRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/set/publish_event`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 获取运单始发地地址 */
export const apiGetWaybillOriginAddress = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillOriginAddressRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/waybill_origin_address`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 获取运单目的地地址 */
export const apiGetWaybillDestinationAddress = async (
  req: IGetDetailReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IWaybillDestinationAddressRes> => {
  return axios.post(`/tmsapi/waybill_record_v2/waybill_destination_address`, { ...req }, { signal }).then(res => ({
    ...res.$data,
  }))
}

/** 获取地址列表 */
export const apiGetAddressList = async (
  req: IAddressListReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IAddressListRes[]> => {
  return axios.get(`/tmsapi/waybill_record_v2/addressList`, { params: req, signal }).then(res => res.$data || [])
}
